/**
 * 文件名：RepairInController.java
 *
 * 创建人：Fyh - 1009173473@qq.com
 *
 * 创建时间：2019年1月10日 上午10:48:06
 *
 * 版权所有：南京植百汇智能科技服务有限公司
 */
package com.z100h.supplychain.evidence.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.jeeplus.common.config.Global;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.z100h.supplychain.evidence.entity.RepairIn;
import com.z100h.supplychain.evidence.facade.RepairInFacade;
import com.z100h.supplychain.evidence.service.RepairInService;

/**
 * 复壮单Controller
 * @author fyh
 * @version 2019-01-10
 */
@Controller
@RequestMapping(value = "${adminPath}/sc/repairin/repairIn")
public class RepairInController extends BaseController {

	@Autowired
	private RepairInService repairInService;
	
	@Autowired
	private RepairInFacade<RepairIn> repairInFacade;
	
	@ModelAttribute
	public RepairIn get(@RequestParam(required=false) String id) {
		RepairIn entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = repairInService.get(id);
		}
		if (entity == null){
			entity = new RepairIn();
		}
		return entity;
	}
	/**
	 * 复壮单列表页面
	 */
	@RequiresPermissions("sc:repairin:repairIn:list")
	@RequestMapping(value = {"list", ""})
	public String list() {
		return "supplychain/evidence/repairInList";
	}
	
		/**
	 * 复壮单列表数据
	 */
	@ResponseBody
	@RequiresPermissions("sc:repairin:repairIn:list")
	@RequestMapping(value = "data")
	public Map<String, Object> data(RepairIn repairIn, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<RepairIn> page = repairInService.findPage(new Page<RepairIn>(request, response), repairIn); 
		return getBootstrapData(page);
	}

	/**
	 * 查看，增加，编辑复壮单表单页面
	 */
	@RequiresPermissions(value={"sc:repairin:repairIn:view","sc:repairin:repairIn:add","sc:repairin:repairIn:edit"},logical=Logical.OR)
	@RequestMapping(value = "form")
	public String form(RepairIn repairIn, Model model) {
		model.addAttribute("repairIn", repairIn);
		if(StringUtils.isBlank(repairIn.getId())){//如果ID是空为添加
			model.addAttribute("isAdd", true);
			return "supplychain/evidence/repairInForm";
		}
		return "supplychain/evidence/repairInCheckForm";
	}

	/**
	 * 保存复壮单
	 */
	@RequiresPermissions(value={"sc:repairin:repairIn:add","sc:repairin:repairIn:edit"},logical=Logical.OR)
	@RequestMapping(value = "save")
	@ResponseBody
	public Map<String, String> save(RepairIn repairIn, Model model, RedirectAttributes redirectAttributes) throws Exception{
//		if (!beanValidator(model, repairIn)){
//			return form(repairIn, model);
//		}
		//新增或编辑表单保存
		Map<String, String> map=new HashMap<String, String>();
		try {
			repairInFacade.repairInProcess(repairIn.getResourceIds(), repairIn);
			map.put("code","0");
			map.put("message","保存复壮单成功");
			//addMessage(redirectAttributes, "保存复壮单成功");
		} catch (Throwable e) {
			e.printStackTrace();
			map.put("code","1");
			map.put("message",e.getMessage());
		}
		//return "redirect:"+Global.getAdminPath()+"/sc/repairin/repairIn/?repage";
		return map;
	}
	
}